/*
 * Copyright 2016-2021 the original author.All rights reserved.
 * Kingstar(aiteasoft@163.com)
 * The license,see the LICENSE file.
 */

package com.aiteasoft.school.scores.rest;

import static com.automvc.common.service.CommService.hasNull;
import static com.automvc.common.service.CommService.isNullPara;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.teasoft.bee.osql.service.ObjSQLRichService;

import com.aiteasoft.school.scores.entity.Scores;
import com.aiteasoft.school.scores.service.ScoresForDeanService;
import com.automvc.common.jq.PageWarp;
import com.automvc.common.jq.Result;

/**
 * @author AiTeaSoft.com
 * @since  1.9
 * Create on 2021-01-18 18:29:23
 */
@RestController("scoresForDean")
@RequestMapping("scoresForDean")
public class ScoresForDeanRest {  
	@Autowired
	ObjSQLRichService objSQLRichService;

	@Autowired
	ScoresForDeanService scoresForDeanService;

	@RequestMapping("/list")
	public Result list(Scores scores, 
			@RequestParam(value = "page", defaultValue = "1", required = false) int page,
			@RequestParam(value = "rows", defaultValue = "20", required = false) int rows) {

		Result result = new Result();
		if (isNullPara(result, scores)) return result;
		
		scores = scoresForDeanService.process(scores);
		
		PageWarp pageWarp = scoresForDeanService.countAndSelect(scores, (page - 1) * rows, rows);
		result.setTotal(pageWarp.getTotal());
		result.setRows(pageWarp.getList());

		return result;
	}

	@RequestMapping("/edit")
	public Result update(Scores scores) {

		Result result = new Result();
		if (isNullPara(result, scores)) return result;
		if(scoresForDeanService.isWrongBizForUpdate(result, scores)) return result;

		String checkFields = "classno,term,examno,subjectno,subject,stuno,name,teacherno,teachername,creater,status,";
		if (hasNull(result, scores, checkFields)) return result;
		
		scores = scoresForDeanService.process(scores);
		
		int num = scoresForDeanService.update(scores);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("update failed!");
		return result;
	}

	@RequestMapping("/del")
	public Result delete(String ids) {

		Result result = new Result();
		if (isNullPara(result, ids, "ids")) return result;
        //TODO 没有权限,直接报异常,跳异常页
		int num = objSQLRichService.deleteById(Scores.class, ids);
		result.setTotal(num);
		if (num <= 0) result.setErrorMsg("delete failed!");
		return result;
	}
}